package Ecras;

import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import Controladores.CtlFotografias;
import Controladores.CtlPermissoes;
import Controladores.NecessitaSerProprietario;

public class EcraFotografias extends JComponent {

	private static final long serialVersionUID = 1L;
	private static final int IMG_X = 130;
	private static final int IMG_Y = 146;
	private JFrame frame;
	private JTextField textColocado_Por;
	private JTextField text_em;
	private JTextField text_Estabelecimento;
	private JTextField textPrato;
	private CtlFotografias ctlFotografias;
	private CtlPermissoes ctlPermissoes;
	private String nome;

	ImageIcon myPicture;
	BufferedImage img;

	public EcraFotografias(String nome, final Integer idEstabelecimento,
			final String estabelecimento, final Integer idPrato,
			final String prato, final ImageIcon fotografia, final String email) {
		frame = new JFrame();
		frame.setBounds(100, 100, 394, 249);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		this.nome = nome;

		ctlFotografias = CtlFotografias.getInstance();
		ctlPermissoes = CtlPermissoes.getInstance();

		JLabel lblColocadoPor = new JLabel("Colocado por:");
		lblColocadoPor.setBounds(10, 41, 107, 14);
		frame.getContentPane().add(lblColocadoPor);

		JLabel lblEm = new JLabel("Em:");
		lblEm.setBounds(10, 66, 107, 14);
		frame.getContentPane().add(lblEm);

		JLabel lblEstabelecimento = new JLabel("Estabelecimento");
		lblEstabelecimento.setBounds(10, 91, 107, 14);
		frame.getContentPane().add(lblEstabelecimento);

		JLabel lblPrato = new JLabel("Prato:");
		lblPrato.setBounds(10, 116, 107, 14);
		frame.getContentPane().add(lblPrato);

		textColocado_Por = new JTextField();
		textColocado_Por.setEditable(false);
		textColocado_Por.setBounds(125, 41, 103, 20);
		if (nome != null) {
			textColocado_Por.setText(nome);
		} else {
			setNome(ctlPermissoes.getNomeUtilizador(email));
		}
		frame.getContentPane().add(textColocado_Por);
		textColocado_Por.setColumns(10);

		text_em = new JTextField();
		text_em.setEditable(false);
		text_em.setBounds(125, 66, 103, 20);
		frame.getContentPane().add(text_em);
		text_em.setColumns(10);

		text_Estabelecimento = new JTextField();
		text_Estabelecimento.setEditable(false);
		text_Estabelecimento.setBounds(125, 91, 103, 20);
		if (idEstabelecimento != null) {
			text_Estabelecimento.setText(estabelecimento);
		}
		frame.getContentPane().add(text_Estabelecimento);
		text_Estabelecimento.setColumns(10);

		textPrato = new JTextField();
		textPrato.setEditable(false);
		textPrato.setBounds(125, 119, 103, 20);
		if (idPrato != null) {
			textPrato.setText(prato);
		}
		frame.getContentPane().add(textPrato);
		textPrato.setColumns(10);

		JLabel image_Panel = new JLabel();
		frame.getContentPane().add(image_Panel);
		image_Panel.setLayout(new FlowLayout());

		System.out.println(idPrato);
		String directory = ctlFotografias.getFotografia(idEstabelecimento,
				idPrato, email);
		char barra = '\\';
		if (directory != null) {
			String directory2 = "";
			for (int i = 0; i < directory.length(); i++) {
				if (directory.charAt(i) == barra) {
					directory2 += "\\\\";
				} else
					directory2 += directory.charAt(i);

			}
			myPicture = new ImageIcon(directory2 + ".gif");
			int racioX = myPicture.getIconWidth() / IMG_X;
			int racioY = myPicture.getIconHeight() / IMG_Y;
			double maxRacio = Math.max(racioX, racioY);
			Image image = myPicture.getImage();
			image = image.getScaledInstance(
					(int) (myPicture.getIconWidth() / maxRacio),
					(int) (myPicture.getIconHeight() / maxRacio),
					java.awt.Image.SCALE_SMOOTH);

			image_Panel.setBounds(238, 14, 130, 146);
			myPicture.setImage(image);
			image_Panel.setIcon(myPicture);
		}

		JButton btnApagar = new JButton("Apagar");
		btnApagar.setBounds(84, 171, 99, 32);
		frame.getContentPane().add(btnApagar);
		btnApagar.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					ctlFotografias.apagarFoto(getNome(), idEstabelecimento,
							idPrato, email);
					JOptionPane.showMessageDialog(null, "Fotografia eliminada");
				} catch (NecessitaSerProprietario e1) {
					JOptionPane.showMessageDialog(null,
							"Nao podes eliminar a fotografia");
				}
			}
		});

		JButton btnUpload = new JButton("Upload");
		btnUpload.setBounds(193, 171, 99, 32);
		frame.getContentPane().add(btnUpload);
		btnUpload.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String directory = JOptionPane
						.showInputDialog("Insira a directoria da fotografia pretendida");
				if (directory != null) {
					if (ctlFotografias.uploadFoto(email, idEstabelecimento,
							idPrato, directory)) {
						JOptionPane.showMessageDialog(null,
								"Fotografia inserida com sucesso");
					}
				}
			}
		});

		frame.setVisible(true);
	}

	private void setNome(String nomeUtilizador) {
		nome = nomeUtilizador;
	}

	public String getNome() {
		return nome;
	}

	public static BufferedImage resize(BufferedImage image, int width,
			int height) {
		BufferedImage bi = new BufferedImage(width, height,
				BufferedImage.TRANSLUCENT);
		Graphics2D g2d = (Graphics2D) bi.createGraphics();
		g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING,
				RenderingHints.VALUE_RENDER_QUALITY));
		g2d.drawImage(image, 238, 14, width, height, null);
		g2d.dispose();
		return bi;
	}
}
